package com.heatup.apt.common.util.Netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;


/**
 * 关闭之前已经引导的客户端或服务器
 *
 * Created by blank on 2017/11/20 上午11:29
 */
public class GracefulShutdown {


    /**
     * 关闭之前已经引导的客户端或服务器
     *
     * 1. 创建 EventLoopGroup 用于处理 I/O
     * 2. 创建一个新的 Bootstrap 并且配置他
     * 3. 最终优雅的关闭 EventLoopGroup 释放资源。这个也会关闭中当前使用的 Channel
     * @throws InterruptedException
     */
    public static void shutdown() throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup(); //1
        Bootstrap bootstrap = new Bootstrap(); //2
        bootstrap.group(group)
                .channel(NioSocketChannel.class);
        //...
        //...
        Future<?> future = group.shutdownGracefully();
        //block until the group has shutdown
        future.sync();

    }



}
